<?php

class WithdrawModel extends Model{

    public $Model = array();

    public function _initialize(){
        $this->Model['Member'] = new MemberModel();
        $this->Model['Currency'] = new CurrencyModel();
        $this->Model['Finance'] = new FinanceModel();
        $this->Model['PayModel'] = new PayModel();
    }

    /**
     * [out 前台提交提现申请]
     * @param  [type]  $member_id   [description]
     * @param  [type]  $money       [description]
     * @param  [type]  $fee         [description]
     * @param  [type]  $bank_id     [description]
     * @param  integer $currency_id [description]
     * @return [type]               [description]
     */
    public function out($member_id,$money,$fee,$bank_id='',$currency_id=0,$status=0,$url=''){
        $this->startTrans();
        $withdraw_fee = $fee;
        // $withdraw_fee = $money*($fee/100);
        $data = array(
            'member_id' =>$member_id,
            'currency_id' =>$currency_id,
            'all_num' =>$money,
            'withdraw_fee' =>$withdraw_fee,
            'num' =>($money-$withdraw_fee),
            'add_time'=>time(),
            'bank_id'=>$bank_id,
            'url'=>$url,
            'status'=>$status
        );
        $num = $this->Model['Currency']->getUserMoney($member_id,$currency_id,'num');
        if($money>$num){
            $r[] = false;
        }
        $r[] = $this->Model['Currency']->setUserMoney($member_id, $currency_id,$money , 'dec', 'num');
        $r[] = $this->Model['Currency']->setUserMoney($member_id,$currency_id, $money, 'inc', 'forzen_num');
        $withdraw_id = $this->add($data);
        $r[] = $withdraw_id;
        //更新
        if(!in_array(false, $r)){
            $this->commit();
            return $withdraw_id;
        }else{
            $this->rollback();
            return false;
        }

    }

    /**
     * [setStatus 设置状态，并扣除或退还用户币种金额]
     * @param [type]  $withdraw_id [description]
     * @param integer $status      [description]
     * @param integer $admin_uid   [description]
     * @param string  $remarks     [description]
     */
    public function setStatus($withdraw_id,$status=1,$admin_uid=0,$remarks=''){
        if(!in_array($status,array(1,2,3,4,5))){
            return false;
        }
        $this->startTrans();
        $withdraw = $this->where(array('withdraw_id'=>$withdraw_id))->find();
        $currency = $this->Model['Currency']->currency_one($withdraw['currency_id'],'rpc_user,rpc_pwd,rpc_url,port_number,rpc_key,rpc_open');
        $forzen_num = $this->Model['Currency']->getUserMoney($withdraw['member_id'],$withdraw['currency_id'],'forzen_num');
        // if($withdraw['status']!=3){
        //     $r[] = false;
        // }
        if($withdraw['all_num']>$forzen_num){
            $r[] = false;
        }
        if($status==4){
            $r[] = $this->Model['Currency']->setUserMoney($withdraw['member_id'],$withdraw['currency_id'], $withdraw['all_num'] , 'dec', 'forzen_num');
            $r[] = $this->Model['Finance']->addFinance($withdraw['member_id'], 'WITHDRAW_FEE', '提现手续费',$withdraw['withdraw_fee'], 2, $withdraw['currency_id']);
            if($currency['rpc_open']==1){
                $r[] = $this->Model['PayModel']->wallet_withdraw($withdraw['url'],$withdraw['num'],$currency);
            }
        }else if($status==2 || $status==5){
            $r[] = $this->Model['Currency']->setUserMoney($withdraw['member_id'],$withdraw['currency_id'], $withdraw['all_num'] , 'inc', 'num');
            $r[] = $this->Model['Currency']->setUserMoney($withdraw['member_id'],$withdraw['currency_id'], $withdraw['all_num'] , 'dec', 'forzen_num');
        }
        $data['withdraw_id'] = $withdraw_id;
        $data['status'] = $status;
        $data['remarks'] = $remarks;
        $data['admin_uid'] = $admin_uid;
        $data['up_time'] = time();
        $r[] = $this->where(array('withdraw_id'=>$withdraw_id,'up_time'=>$withdraw['up_time']))->save($data);
        //更新订单状态
        if(!in_array(false, $r)){
            $this->commit();
            return true;
        }else{
            $this->rollback();
            return false;
        }
    }
    
}